package com.test;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.python.core.*;
import org.python.util.PythonInterpreter;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;

/**
 * @author hugh.sun
 * @date 2021/11/17 11:56
 */
@Slf4j
public class jython {
    /*创建pythonInterpreter调用python*/
    private static PythonInterpreter pythonInterpreter = new PythonInterpreter();

    @Test
    void test1() {

        /*选中python文件，*/
        pythonInterpreter.execfile("src/main/resources/main.py");

        PyFunction function = pythonInterpreter.get("bubbleSort", PyFunction.class);

        ArrayList<Integer> objects = new ArrayList<>();

        Random random = new Random();

        for (int i = 0; i < 20; i++) {
            int i1 = random.nextInt(200);
            objects.add(i1);
        }

        PyObject pyObject = function.__call__(new PyList());

        log.debug(pyObject.toString()+"--------log打印");

        System.out.println(pyObject+"------------输出");

    }
    @Test void test2(){
        /*获取uuid*/
       /* String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
        System.out.println(uuid);*/
        /*修改时间格式*/
        Date date = new Date();
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        System.out.println(sdf.format(date));
    }
    @Test void test3() throws InterruptedException, IOException {
        String exe = "python";
        String command = "E:\\项目-estore\\day70\\springCloud\\sprintboot-redis\\src\\main\\resources\\main.py";
        String num1 = "1";
        String num2 = "2";
        String[] cmdArr = new String[] {exe, command,num1, num2};
        Process process = null;
        try {
            process = Runtime.getRuntime().exec(cmdArr);

        } catch (IOException e) {
            e.printStackTrace();
        }

        InputStream is = process.getInputStream();
        DataInputStream dis = new DataInputStream(is);
        String str = dis.readLine();
        process.waitFor();
       System.out.println(str);

    }
    @Test
    void test4(){

        String exe = "python";
        String command = "src/main/resources/recotest.py";
        String s = "E:\\picture\\girl.jpg";
        String[] cmdArr = new String[] {exe, command,s};
        Process process = null;
        try {
            process = Runtime.getRuntime().exec(cmdArr);

        } catch (IOException e) {
            e.printStackTrace();
        }

        InputStream is = process.getInputStream();

        DataInputStream dis = new DataInputStream(is);
        try {
            System.out.println(dis.readLine());
        } catch (IOException e) {
            e.printStackTrace();
        }
        /* String str = dis.readLine();*/
        try {
            process.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        /*  System.out.println(str);*/
    }
    @Test
    void test5() throws IOException, InterruptedException {
        String exe = "E:\\Program Files\\python\\python.exe";
        String command = "E:\\项目-estore\\day70\\springCloud\\sprintboot-redis\\src\\main\\resources\\main.py";
        String num1 = "1";
        String num2 = "2";
        String[] cmdArr = new String[] {exe, command, num1, num2};
        Process process = Runtime.getRuntime().exec(cmdArr);
        InputStream is = process.getInputStream();
        DataInputStream dis = new DataInputStream(is);
        String str = dis.readLine();
        process.waitFor();
        System.out.println(str);
    }
    @Test
    public void test6(){

        String pythonFunc = "E:\\项目-estore\\day70\\springCloud\\sprintboot-redis\\src\\main\\resources\\recotest.py";

        PythonInterpreter pi1 = new PythonInterpreter();
        // 加载python程序
        pi1.execfile(pythonFunc);
        // 调用Python程序中的函数
        PyFunction pyf = pi1.get("recognition", PyFunction.class);

        Py.newString("E:\\picture\\girl.jpg");

        PyObject dddRes = pyf.__call__(Py.newString("E:\\picture\\girl.jpg"));
        System.out.println(dddRes);
        pi1.cleanup();
        pi1.close();


    }

    }




